<?
	include('wallet_db.html');
	//------------------------------------------------------
		$sql['type']='SELECT';
		$sql['params']="product_id, name, plan_id";
		$sql['table']='products';
		$sql['where']="reseller_id='$id' AND type='plan' AND plan_id != 'a'";
		$sql['options']='';
		$sql['return']='struct';
	//------------------------------------------------------
	if (!$plres=sql_exec($sql,$dbh81,'assoc')) {
		$errorMsg = translate('int_error');
		include_once('errorMsg.html');
		exit();
	}
	foreach ($plres as $value) {
		if ($value['plan_id'] == 'b') {
			if ($value['name'] != 'Business') {
				//------------------------------------------------------
				$sql['type']='SELECT';
				$sql['params']='count(distinct pu.client_id) as count';
				$sql['table']='purchases as pu, products as pr, clients as c';
				$sql['where']="pr.product_id='".$value['product_id']."' and pr.product_id=pu.product_id and pu.client_id=c.client_id and c.blocked=0";
				$sql['options']='group by pu.product_id';
				$sql['return']='struct';
				//------------------------------------------------------
				if (!$result=sql_exec($sql,$dbh81,'assoc')) {
					$business_count = '0';
				} else {
					$business_count = $result[0]['count'];
				}
				$business_changed = 1;
				$business_id = $value['product_id'];
				$business_name = $value['name'];
			}
		}
		if ($value['plan_id'] == 'c') {
			if ($value['name'] != 'Corporate') {
				//------------------------------------------------------
				$sql['type']='SELECT';
				$sql['params']='count(distinct pu.client_id) as count';
				$sql['table']='purchases as pu, products as pr, clients as c';
				$sql['where']="pr.product_id='".$value['product_id']."' and pr.product_id=pu.product_id and pu.client_id=c.client_id and c.blocked=0";
				$sql['options']='group by pu.product_id';
				$sql['return']='struct';
				//------------------------------------------------------
				if (!$result=sql_exec($sql,$dbh81,'assoc')) {
					$corporate_count = '0';
				} else {
					$corporate_count = $result[0]['count'];
				}
				$corporate_changed = 1;
				$corporate_id = $value['product_id'];
				$corporate_name = $value['name'];
			}
		}
	}
	if (isset($business_changed) or isset($corporate_changed)) {
		$disa = 'disabled';
	} else {
		$disa = '';
	}
	$params[sid] = $sid;
	$params[action] = "editTemplate";
	$url = "$baseURL/?".encode_params($params);
	$data = get_interface_settings($id);
	
	// sve: patch
	if (!empty($template)) {
		$data['template'] = $template;
	}
	
	if(is_array($data)) {
		while(list($k,$v) = each($data)){
			$$k = $v;
		}
	}
	
	/*
	switch($template) {
		case "0": $ch[1] = "checked"; break;
		case "1": $ch[2] = "checked"; break;
		case "2": $ch[3] = "checked"; break;
		case "3": $ch[4] = "checked"; break;
		case "4": $ch[5] = "checked"; break;
		case "5": $ch[6] = "checked"; break;
		case "6": $ch[7] = "checked"; break;
		case "7": $ch[8] = "checked"; break;
		case "8": $ch[9] = "checked"; break;
		case "9": $ch[10] = "checked"; break;
		case "10": $ch[11] = "checked"; break;
		case "11": $ch[12] = "checked"; break;
		case "12": $ch[13] = "checked"; break;
		case "13": $ch[14] = "checked"; break;
		case "14": $ch[15] = "checked"; break;
                case "15": $ch[16] = "checked"; break;
	}
	*/
	// sve: company name patch
	$resellerDetails = list_details($id, 'array');
	$resellerDetails = $resellerDetails[0];
	
	if (empty($resellerDetails['company_name'])) {
		$activateJs = true;	
	}
?>


<table width="450">
  <tr>
    <td colspan=2><font class="text"><b><?=translate('template_manager');?></b><br>
      <br>
      <?=translate('template_manager_desc');?>
      </font></td>
  </tr>
</table>
<br>
<?
	if ($errorMsg) {
		include_once('errorMsg.html');
	}
	
	if ($okMsg) {
		include_once('okMsg.html');
	}
?>
<script>

<?php
	if ($activateJs === true) {
		echo 'var checkAlert = true;';	
	}
	else {
		echo 'var checkAlert = false;';
	}

?>
function change_img(el){
	limage=document.getElementById('large_image');

	limage.src='images/r'+el.value+'_big.jpg';
}

// sve: checkForm () will be used mainly when the reseller does not have company name
// this is applicable only for template 15
function checkForm() {
	// form object
	var formObject = this.form;
	var elObject = document.getElementById('companyname_div');
	
	// exit if the form Object is undefined
	if (typeof(formObject) == 'undefined') {
		return false;	
	}
	
	// do not show company name input if reseller's company name is okay
	for (var i = 0; i < formObject.template.length; i++) {
		if (formObject.template[i].type == 'radio' && formObject.template[i].value == companyNameForTemplate) {
			if (checkAlert == false || formObject.template[i].checked == false) {
				elObject.style.display = 'none';
				return true;
			}
		}
	}
	
	// activate company name input
	elObject.style.display = 'block';
	
	if (formObject.company_name.value == '') {
		if (errorCode != 1) {
			alert('<?php echo translate('warning_message'); ?>');
		}
		formObject.company_name.focus();
	}
	
	return true;
}

function addProperties (formObject) {
	// exit if the form Object is undefined
	if (typeof(formObject) == 'undefined') {
		return false;	
	}
	
	for (var i = 0; i < formObject.template.length; i++) {
		if (formObject.template[i].type == 'radio') {
			formObject.template[i].onclick = checkForm;
		} 
	}
}

function checkRadio (templateNum) {
	if (typeof(templateNum) == 'undefined') {
		return false;
	}
	
	for (var i = 0; i < formObj.template.length; i++) {
		if (formObj.template[i].type == 'radio' && formObj.template[i].value == templateNum) {
			formObj.template[i].click();
		}
	}
}
</script>
<br />
<?
if (isset($business_changed)) {
	unset($params);
	$params['sid'] = $sid;
	$params['action'] = "managePlans";
	$params['sub'] = 'edit';
	$params['prod_id'] = $business_id;
	$params['plname'] = $business_name;
	$params['prebuild'] = 'yes';
	$params['cl_count'] = $business_count;
	$resurl = "$baseURL/?".encode_params($params);
	echo "<div align='left'>
	WARNING: Your business plan name is changed. Only the newest template is available for use!
	<input type='button' name='res_butt' value='Reset Business Plan' onClick='window.location=\"$resurl\";'>
	</div>";
} elseif (isset($corporate_changed)) {
	unset($params);
	$params['sid'] = $sid;
	$params['action'] = "managePlans";
	$params['sub'] = 'edit';
	$params['prod_id'] = $corporate_id;
	$params['plname'] = $corporate_name;
	$params['prebuild'] = 'yes';
	$params['cl_count'] = $corporate_count;
	$resurl = "$baseURL/?".encode_params($params);
	echo "<div align='left'>
	WARNING: Your corporate plan name is changed. Only the newest template is available for use!
	<input type='submit' name='submit' value='Reset Corporate Plan' onClick='window.location=\"$resurl\";'>
	</div>";
}
?>
<br>
<TABLE border=1 class='bTable' cellspacing='0' width='560'>
	<form action='<?=$url;?>' method='POST' id='form_name'>
	<TR>
		<TH colspan='2'><?=translate('template_manager');?></TH>
	</TR>
	<TR>
		<TD align='center'>
			<table border=0 width='100%' style="border-left: 1px solid #FFC9AD;">
				<TR>
					<TH colspan='4' align="left">New Templates - Recommended</TH>
				</TR>
				<tr align='center'>
					<td valign='bottom'>
						<img width=90 src='images/r15.jpg'>
					</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
				<tr align='center'>
					<td><input type='radio' name='template' value='15'> #16</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
				<TR>
					<TD  align='center' <?php if ($errorCode == 1) { echo 'style="color: red;"'; }?>>
						<div id='companyname_div' style='display: none'>Company Name:<br><input type='text' id='company_name' name='company_name' value='' size='15' <?php if ($errorCode == 1) { echo 'style="border: 1px solid red;"'; }?>></div>
					</TD>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</TR>
				<TR>
					<TH colspan='4' align="left">Old Templates</TH>
				</TR>
				<tr align='center'>
					<td valign='bottom'>
						<img src='images/langs.jpg'>&nbsp;
						<img width=90 src='images/r0.jpg'>
					</td>
					<td valign='bottom'>
						<img src='images/langs.jpg'>&nbsp;
						<img width=90 src='images/r1.jpg'>
					</td>
					<td>
						<img width=90 src='images/r2.jpg'>
					</td>
					<td>
						<img width=90 src='images/r3.jpg'>
					</td>
				</tr>
				<tr align='center'>
					<td>
						<input type='radio' id='template' name='template' value='0' <?=$disa;?>> #1
					</td>
					<td>
						<input type='radio' id='template' name='template' value='1' <?=$disa;?>> #2
					</td>
					<td>
						<input type='radio' id='template' name='template' value='2' <?=$disa;?>> #3
					</td>
					<td>
						<input type='radio' id='template' name='template' value='3' <?=$disa;?>> #4
					</td>
				</tr>
				<tr align='center'>
					<td>
						<img width=90 src='images/r4.jpg'>
					</td>
					<td>
						<img width=90 src='images/r5.jpg'>
					</td>
					<td>
						<img width=90 src='images/r6.jpg'>
					</td>
					<td>
						<img width=90 src='images/r7.jpg'>
					</td>
				</tr>
				<tr align='center'>
					<td>
						<input type='radio' id='template' name='template' value='4' <?=$disa;?>> #5
					</td>
					<td>
						<input type='radio' id='template' name='template' value='5' <?=$disa;?>> #6
					</td>
					<td>
						<input type='radio' id='template' name='template' value='6' <?=$disa;?>> #7
					</td>
					<td>
						<input type='radio' id='template' name='template' value='7' <?=$disa;?>> #8
					</td>
				</tr>
				<tr align='center'>
					<td>
						<img width=90 src='images/r8.jpg'>
					</td>
					<td>
						<img width=90 src='images/r9.jpg'>
					</td>
					<td>
						<img width=90 src='images/r10.jpg'>
					</td>
					<td valign='bottom'>
						<img src='images/langs.jpg'>&nbsp;
						<img width=90 src='images/r11.jpg'>
					</td>
				</tr>
				<tr align='center'>
					<td>
						<input type='radio' id='template' name='template' value='8' <?=$disa;?>> #9
					</td>
					<td>
						<input type='radio' id='template' name='template' value='9' <?=$disa;?>> #10
					</td>
					<td>
						<input type='radio' id='template' name='template' value='10' <?=$disa;?>> #11
					</td>
					<td>
						<input type='radio' id='template' name='template' value='11' <?=$disa;?>> #12
					</td>
				</tr>
				<tr align='center'>
					<td valign='bottom'>
						<img src='images/langs.jpg'>&nbsp;
						<img width=90 src='images/r12.jpg'>
					</td>
					<td valign='bottom'>
						<img src='images/langs.jpg'>&nbsp;
						<img width=90 src='images/r13.jpg'>
					</td>
					<td valign='bottom'>
						<img src='images/langs.jpg'>&nbsp;
						<img width=90 src='images/r14.jpg'>
					</td>
					<td>&nbsp;</td>
				</tr>
				<tr align='center'>
					<td>
						<input type='radio' id='template' name='template' value='12' <?=$disa;?>> #13
					</td>
					<td>
						<input type='radio' id='template' name='template' value='13' <?=$disa;?>> #14
					</td>
					<td>
						<input type='radio' id='template' name='template' value='14' <?=$disa;?>> #15
					</td>
					<td>&nbsp;</td>
				</tr>
			</table>
		</TD>
	</TR>
	<TR>
		<TD align='center' colspan='2'>
			<input type='submit' value='<?=translate('submit');?>'>
		</TD>
	</TR>	
</form>
</TABLE>
<table width="450">
	<tr>
		<td colspan="2">
			<font class="text"><br><br>
			The flags, next to the template thumbnail, represent the languages the template can be translated into using the drop-down menu.<br><br>
			Country flags legend : <br>
			<br>
			<img src='images/en.jpg'> - English <br>
			<img src='images/pt.jpg'> - Portuguese <br>
			<img src='images/fr.jpg'> - French <br>
			<img src='images/es.jpg'> - Spanish
			</font>
		</td>
	</tr>
</table>
<script language="Javascript">
var companyNameForTemplate = 15;
var errorCode = <?php echo !empty($errorCode) ? 1 : 0;?>;
var formObj = document.getElementById('form_name');
addProperties(formObj);
checkRadio(<?php echo $template;?>);
</script>
